package com.kimo.shop.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kimo.shop.bean.model.RefundOrderSettlement;
import com.kimo.shop.dao.OrderRefundMapper;
import com.kimo.shop.dao.RefundOrderSettlementMapper;
import com.kimo.shop.service.RefundOrderSettlementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class RefundOrderSettlementServiceImpl extends ServiceImpl<RefundOrderSettlementMapper, RefundOrderSettlement> implements RefundOrderSettlementService {

    @Autowired
    private OrderRefundMapper orderRefundMapper;

    @Autowired
    private RefundOrderSettlementMapper refundOrderSettlementMapper;

    /*@Override
    public void refundOrderSettlement(List<RefundOrderSettlement> refundOrderSettlementList) {
        try {
            for (RefundOrderSettlement refundOrderSettlement : refundOrderSettlementList) {
                WxPayRefundRequest wxPayRefundRequest = new WxPayRefundRequest();
                wxPayRefundRequest.setTotalFee(refundOrderSettlement.getOrderTotalAmount().multiply(new BigDecimal(Integer.toString(100))).intValue());
                wxPayRefundRequest.setRefundFee(refundOrderSettlement.getRefundAmount().multiply(new BigDecimal(Integer.toString(100))).intValue());
                wxPayRefundRequest.setOutRefundNo(refundOrderSettlement.getRefundPayNo());
                wxPayRefundRequest.setOutTradeNo(refundOrderSettlement.getOrderPayNo());
                wxPayRefundRequest.setRefundDesc("商品退款");
                wxMpPayService.refund(wxPayRefundRequest);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void queryRefundOrderSettlement(List<RefundOrderSettlement> refundOrderSettlementList) {
        try {
            for (RefundOrderSettlement refundOrderSettlement : refundOrderSettlementList) {
                WxPayRefundQueryRequest wxPayRefundQueryRequest = new WxPayRefundQueryRequest();
                wxPayRefundQueryRequest.setOutRefundNo(refundOrderSettlement.getRefundPayNo());
                WxPayRefundQueryResult wxPayRefundQueryResult = wxMpPayService.refundQuery(wxPayRefundQueryRequest);
                List<WxPayRefundQueryResult.RefundRecord> refundRecords = wxPayRefundQueryResult.getRefundRecords();
                String refundStatus = refundRecords.get(0).getRefundStatus();
                if (Objects.equals(refundStatus, "SUCCESS")) {
                    // 更新为已完成状态
                    int row = refundOrderSettlementMapper.refundSuccess(refundOrderSettlement.getRefundPayNo(), refundOrderSettlement.getVersion());
                    if (row != 1) {
                        throw new KimoShopBindException("更新退款订单失败");
                    } else {
                        orderRefundMapper.updateRefundApplyStsByRefundSn(refundOrderSettlement.getRefundSn(), RefundApplyStsType.SUCCESS.value());
                    }
                } else if (Objects.equals(refundStatus, "FAIL")) {
                    // 更新为失败状态
                    int row = refundOrderSettlementMapper.refundFail(refundOrderSettlement.getRefundPayNo(), refundOrderSettlement.getVersion());
                    if (row != 1) {
                        throw new KimoShopBindException("更新退款订单失败");
                    } else {
                        orderRefundMapper.updateRefundApplyStsByRefundSn(refundOrderSettlement.getRefundSn(), RefundApplyStsType.FAIL.value());
                    }
                } else if (Objects.equals(refundStatus, "PROCESSING")) {
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

    } }*/


}
